<?php
/**
 * Created by PhpStorm.
 * User: miidi
 * Date: 2018/9/27
 * Time: 11:56
 */

namespace app\wx\controller;


use app\wx\model\Collection;
use app\wx\model\XcxUserInfo;

class LookCollection
{
  public function addCollection()
  {
    $data = request()->param();
    $historyModel = new Collection();
    $findResult = $historyModel->where('lookHouseid', $data['lookHouseid'])->find();
    if (!$findResult) {
      //模型->save  就是新增
      $historyModel->save($data);
    } else {
      //模型的查询结果-> save  就是update
      $findResult->save($data);
    }
    return json([
        'code' => 0,
        'message' => 'ok'
    ]);
  }


  public function cancelCollection()
  {
    $data = request()->param();
    $historyModel = new Collection();
    $findResult = $historyModel->where('lookHouseid', $data['lookHouseid'])
        ->where('userId', $data['userId'])
        ->delete();
    if ($findResult) {
      return json([
          'code' => 0,
          'message' => '取消成功'
      ]);
    }

  }

  public function isCollection()
  {
    $data = request()->param();
    $historyModel = new Collection();
    $findResult = $historyModel->where('lookHouseid', $data['lookHouseid'])
        ->where('userId', $data['userId'])
        ->find();
    if ($findResult) {
      return json([
          'code' => 0,
          'message' => '该用户收藏了该房子'
      ]);
    }
  }

  public function getCollection()
  {
    $userId = input('userId');
    $result = XcxUserInfo::getCollectionById($userId);
    if ($result) {
      return json([
          'code' => 0,
          'data' => $result
      ]);
    }
  }

}